package model;

import java.io.*;

public class ModelUtilities {

	public static String[] ReadFile(String location) {
		String[] lines;
		try {
			lines = new String[count(location)+1];
		} catch (IOException e1) {
			e1.printStackTrace();
			return null;
		}
		
	    try {
	      FileInputStream fstream = new FileInputStream(location);
	      DataInputStream in = new DataInputStream(fstream);
	      BufferedReader br = new BufferedReader(new InputStreamReader(in));
	      String str;
	      int count = 0;
	      System.out.println(count);
	      while ((str = br.readLine()) != null) {
	        lines[count] = str;
	        count++;
	      }
	      in.close();
	    } catch (Exception e) {
	    	System.err.println(e);
	    	System.out.println(": (");
	    	return null; 
	    }
	    System.out.println(": )");
	    return lines;
	  }
	
	// Utility to QUICKLY count the number of lines in a file: 
	// (rather than using readLine() twice or appending to an array; both of which are slow)
	public static int count(String filename) throws IOException {
	    InputStream is = new BufferedInputStream(new FileInputStream(filename));
	    try {
	        byte[] c = new byte[1024];
	        int count = 0;
	        int readChars = 0;
	        while ((readChars = is.read(c)) != -1) {
	            for (int i = 0; i < readChars; ++i) {
	                if (c[i] == '\n')
	                    ++count;
	            }
	        }
	        return count;
	    } finally {
	        is.close();
	    }
	}

	
	
}
